import regeneratorRuntime from '../../../utils/regeneratorRuntime.js'

var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: '',
    order_id: 0
  },

  onLoad: function (options) {
    var order_id = options.order_id;
    if (order_id) {
      this.setData({
        order_id: order_id
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAddress();
  },

  chooseAddress: function (e) {
    var id = e.currentTarget.dataset.id;
    if (!this.data.order_id) {
      return false;
    }
    var address = {};
    this.data.list.forEach((item) => {
      if (id === item.id) {
        address = item;
      }
    });
    wx.setStorageSync('curr_address', address);
    wx.navigateBack({
      delta: 1
    })
  },

  async getAddress () {
    try {
      const list = await app.address.getAddress()
      this.setData({ list })
    } catch (err) {
      app.toast(err)
    }
  },

  goCreate: function() {
    var order_id = this.data.order_id;
    wx.navigateTo({
      url: 'create?order_id=' + order_id
    })
  },

  goEdit: function (e) {
    var id = e.currentTarget.dataset.id
    var order_id = this.data.order_id;
    wx.navigateTo({
      url: 'create?order_id=' + order_id + '&id=' + id
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})